<template>
  <div class="container">
    <div class="top">
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        class="add"
        icon="el-icon-refresh"
        size="small"
        type="success"
        @click="refresh"
        >刷新</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column label="xtbh" align="center">
        <el-table-column
          prop="xtbh"
          label="系统编号"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="djbh" align="center">
        <el-table-column
          prop="serial_code"
          label="采购入库编号"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="orgbh" align="center">
        <el-table-column
          prop="org_area_code"
          label="主体编码"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="org" align="center">
        <el-table-column
          prop="org_area_name"
          label="主体"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="rq" align="center">
        <el-table-column
          prop="rq"
          label="单据审核日期"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="zdr" align="center">
        <el-table-column
          prop="zdr"
          label="登录人"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="bmbh" align="center">
        <el-table-column
          prop="drbmbh"
          label="门店编码"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="bm" align="center">
        <el-table-column
          prop="drbm"
          label="门店名称"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="drbmbh" align="center">
        <el-table-column
          prop="erp_org_code"
          label="门店区域编码"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="drbm" align="center">
        <el-table-column
          prop="org_name"
          label="门店区域名称"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="plbh" align="center">
        <el-table-column
          prop="category_code_one"
          label="品类编码"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="pl" align="center">
        <el-table-column
          prop="category_name_one"
          label="品类"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="dcbmbh" align="center">
        <el-table-column
          prop="dcbmbh"
          label="仓库编码"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="dcbm" align="center">
        <el-table-column
          prop="dcbm"
          label="发货仓库"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="je" align="center">
        <el-table-column
          prop="je"
          label="成本金额"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="fyje" align="center">
        <el-table-column
          prop="fyje"
          label="预提"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="yzj" align="center">
        <el-table-column
          prop="yzj"
          label="溢折"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="zhy" align="center">
        <el-table-column
          prop="zhy"
          label="摘要"
          align="center"
        ></el-table-column>
      </el-table-column>

      <el-table-column label="dhtc" align="center">
        <el-table-column
          prop="dhtc"
          label="调货提成"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="kjj" align="center">
        <el-table-column
          prop="kjj"
          label="筐加价"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="cgtc" align="center">
        <el-table-column
          prop="cgtc"
          label="采购提成"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="bz" align="center">
        <el-table-column
          prop="bz"
          label="空字段"
          align="center"
        ></el-table-column>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
  </div>
</template>
<script>
import { getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import { saleCertification } from "@/api/voucher/index";
import { getToken } from "@/utils/auth";

export default {
  components: {
    pagination: Pagination,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        start_time: "",
        end_time: "",
      },
      date: [],
    };
  },
  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    // this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        start_date: this.search.start_time,
        end_date: this.search.end_time,
      };

      saleCertification(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.data.paginate.data;
            this.pageData.page = res.data.data.paginate.current_page;
            this.pageData.pagesize = res.data.data.paginate.per_page;
            this.pageData.total = res.data.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 本地搜索
    handleSearch() {
      if (this.date) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    // 导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href = `${
            this.$axios.defaults.baseURL
          }/smkapi/Certification/exportSaleCertification?token=${getToken()}&start_date=${
            this.search.start_time
          }&end_date=${this.search.end_time}`;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //刷新
    refresh() {
      this.empty();
      this.getData();
    },
    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
      };
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>